package com.qdlc.p2p.manage.action.additional;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.NewActivityPlanService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.NewActivityPlan;
import com.qdlc.p2p.dal.dto.Operator;
import com.qdlc.p2p.dal.model.NewActivityPlanModel;

/**
 * Created by liaoxiangkai on 2015/11/28.
 */
public class ManageNewActivityPlanAction extends BaseAction<NewActivityPlanModel> implements ModelDriven<NewActivityPlanModel> {
    @Resource
    private NewActivityPlanService newActivityPlanService;

    private NewActivityPlanModel model = new NewActivityPlanModel();

    public NewActivityPlanModel getModel() {
        return model;
    }

    private Map<String, Object> data;

    /**
     * ---------------活动主页---------------------
     * 进入页面
     *
     * @return
     * @throws Exception
     */
    @Action("/modules/additional/newActivityPlan/newActivityPlanManager")
    public String newActivityPlanManager() throws Exception {
        return "newActivityPlanManager";
    }

    /**
     * 列表
     *
     * @throws Exception
     */
    @Action("/modules/additional/newActivityPlan/newActivityPlanList")
    public void newActivityPlanList() throws Exception {
        data = new HashMap<String, Object>();
        PageDataList<NewActivityPlanModel> pageDataList = newActivityPlanService.list(model);
        if (pageDataList == null) {
            return;
        }
        data.put("total", pageDataList.getPage().getTotal());
        data.put("rows", pageDataList.getList());
        printJson(getStringOfJpaObj(data));
    }

    /**
     * --------------------添加活动--------------------------
     * 进入页面
     *
     * @return
     * @throws Exception
     */
    @Action("/modules/additional/newActivityPlan/newActivityPlanAddPage")
    public String newActivityPlanAddPage() throws Exception {
        return "newActivityPlanAddPage";
    }

    /**
     * 活动添加动作
     *
     * @throws Exception
     */
    @SuppressWarnings("static-access")
    @Action("/modules/additional/newActivityPlan/newActivityPlanAdd")
    public void newActivityPlanAdd() throws Exception {
        Operator operator = getOperator();
        if (null == operator) {
            printResult("请先登录！", false);
            return;
        }
        //检测添加活动参数
        if (!checkNewActivityPlanRule(model) || !checkCanBeAdd(model)){
            printWebResult("一个环节只能开启一条活动方案！", false);
            return;
        }
        //设置创建者创建时间
        model.setCreator(operator);
        model.setCreateTime(DateUtil.getNow());

        NewActivityPlan newActivityPlan = newActivityPlanService.save(model);
        if (newActivityPlan == null) {
            printResult("添加活动失败！", false);
            return;
        }
        printResult("添加活动成功！", true);
    }

    /**
     * -------------------编辑活动-----------------------------
     * 进入页面
     *
     * @return
     * @throws Exception
     */
    @Action("/modules/additional/newActivityPlan/newActivityPlanEditPage")
    public String newActivityPlanEditPage() throws Exception {
        NewActivityPlanModel activityPlan = newActivityPlanService.find(model.getId());
        if (activityPlan == null) {
            printResult("该活动不存在!", false);
            return "newActivityPlanManager";
        }
        request.setAttribute("activityPlan", activityPlan);
        return "newActivityPlanEditPage";
    }

    /**
     * 活动更新动作
     *
     * @throws Exception
     */
    @Action("/modules/additional/newActivityPlan/doNewActivityPlanEdit")
    public void doNewActivityPlanEdit() throws Exception {
        Operator operator = getOperator();
        if (null == operator) {
            printResult("请先登录！", false);
            return;
        }
        //检测添加活动参数
        NewActivityPlan plan = newActivityPlanService.find(model.getId());
        if (null != plan && plan.getStatus() != 0) {
            if (!checkNewActivityPlanRule(model) || !checkCanBeAdd(model)){
                printWebResult("一个环节只能开启一条活动方案！", false);
                return;
            }
        }
        model.setModifier(operator);
        model.setModifyTime(DateUtil.getNow());
        //更新活动
        NewActivityPlan newActivityPlan = newActivityPlanService.update(model);
        if (null == newActivityPlan) {
            printResult("修改活动失败！", false);
            return;
        }
        printResult("修改活动成功！", true);
    }

    /**
     * ----------------详情页面-------------------
     *
     * @return
     * @throws Exception
     */
    @Action("/modules/additional/newActivityPlan/newActivityPlanDetailPage")
    public String newActivityPlanDetailPage() throws Exception {
        NewActivityPlanModel activityPlan = newActivityPlanService.find(model.getId());
        if (activityPlan == null) {
            printResult("该活动不存在!", false);
            return "newActivityPlanManager";
        }
        request.setAttribute("activityPlan", activityPlan);
        return "newActivityPlanDetailPage";
    }

    /**
     * --------------------是否开启本条规则-----------------------
     * 开启规则
     *
     * @throws IOException
     */
    @Action("/modules/additional/newActivityPlan/newActivityPlanEnable")
    public void newActivityPlanEnable() throws Exception {
        Operator operator = getOperator();
        if (null == operator) {
            printResult("请先登录！", false);
            return;
        }
        NewActivityPlan newActivityPlan = newActivityPlanService.find(model.getId());
        if (null != newActivityPlan)
            model.setTache(newActivityPlan.getTache());
        if (!checkCanBeAdd(model)){
            printWebResult("一个环节只能开启一条活动方案！", false);
            return;
        }
        model.setModifier(operator);
        model.setModifyTime(DateUtil.getNow());
        int result = newActivityPlanService.toggleStatus(model);
        if (result > 0) {
            printWebSuccess();
        } else {
            printWebResult("启用失败，请确认活动是否已开启！", false);
        }
    }

    /**
     * 关闭规则
     *
     * @throws IOException
     */
    @Action("/modules/additional/newActivityPlan/newActivityPlanDisable")
    public void newActivityPlanDisable() throws Exception {
        Operator operator = getOperator();
        if (null == operator) {
            printResult("请先登录！", false);
            return;
        }
        model.setModifier(operator);
        model.setModifyTime(DateUtil.getNow());
        int result = newActivityPlanService.toggleStatus(model);
        if (result > 0) {
            printWebSuccess();
        } else {
            printWebResult("关闭失败，请确认活动是否已关闭！", false);
        }
    }

    /**
     * ------------------校验方法-----------------------
     * 检验添加活动参数
     *
     * @param model
     * @return
     */
    private boolean checkNewActivityPlanRule(NewActivityPlanModel model) throws Exception {
        if (StringUtil.isBlank(model.getName())) {
            printWebResult("请填写活动名称！", false);
            return false;
        }
        if (StringUtil.isBlank(model.getTache())) {
            printWebResult("请选择活动环节！", false);
            return false;
        }
        if (StringUtil.isBlank(model.getStartTime())) {
            printWebResult("请填写活动开始时间！", false);
            return false;
        }
        if (StringUtil.isBlank(model.getEndTime())) {
            printWebResult("请填写活动结束时间！", false);
            return false;
        }
        return true;
    }

    /**
     * 校验每个环节只能有一个可用
     *
     * @param model
     * @return
     */
    private boolean checkCanBeAdd(NewActivityPlanModel model) {
        if (model.getStatus() == 1) return true;
        List<NewActivityPlan> list = newActivityPlanService.isCanBeAdd(model);
        return list.isEmpty();
    }
}
